﻿#include "ex01.h"




double fun01(double x)
{ 
	double ans;
	ans = exp(x);
	return ans;
}

void ex01() {
	double a = 1, b = 3, error = 0.0001, t[20][20], h, c;
	int i, j, k, m, n;
	h = b - a;
	t[0][0] = h * (fun01(a) + fun01(b)) / 2;
	k = 1;
	while (1)
	{
		t[0][k] = 0;
		m = 1;
		for (j = 0; j < k - 1; j++)
			m = m * 2;
		for (i = 1; i <= m; i++)
			t[0][k] = t[0][k] + h * fun01(a + (i - 0.5) * h);
		t[0][k] = (t[0][k] + t[0][k - 1]) / 2;
		for (j = 1; j <= k; j++)
		{
			c = 1;
			for (n = 0; n < j;
				n++) c = c * 4;
			t[j][k - j] = (c * t[j - 1][k - j + 1] - t[j - 1][k - j]) / (c - 1);
		}
		if (fabs(t[k][0] - t[k - 1][0]) < error)
		{
			printf("\n积分结果I =%lf\n", t[k][0]);
			break;
		}
		else
		{
			h = h / 2;
			k++;
		}
	}
}